package com.java110.mall.goods.cmd.productGroup;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.product.ProductGroupOrderReturnDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.goods.IProductGroupOrderReturnV1InnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 商铺查询拼团退单
 */
@Java110Cmd(serviceCode = "productGroup.queryShopRefundGroupOrder")
public class QueryShopRefundGroupOrderCmd extends Cmd {

    private static Logger logger = LoggerFactory.getLogger(QueryShopRefundGroupOrderCmd.class);
    @Autowired
    private IProductGroupOrderReturnV1InnerServiceSMO productGroupOrderReturnV1InnerServiceSMOImpl;
    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateShop(context);
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含店铺编号");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        // 将link 转为personId
        String link = reqJson.getString("link");
        if (!StringUtil.isEmpty(link)) {
            UserDto userDto = new UserDto();
            userDto.setTel(link);
            List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
            if (ListUtil.isNull(userDtos)) {
                return;
            }
            reqJson.put("buyerId", userDtos.get(0).getUserId());
            reqJson.remove("link");
        }
        ProductGroupOrderReturnDto productGroupOrderReturnDto = BeanConvertUtil.covertBean(reqJson, ProductGroupOrderReturnDto.class);

        int count = productGroupOrderReturnV1InnerServiceSMOImpl.queryProductGroupOrderReturnsCount(productGroupOrderReturnDto);

        List<ProductGroupOrderReturnDto> productGroupOrderReturnDtos = null;

        if (count > 0) {
            productGroupOrderReturnDtos = productGroupOrderReturnV1InnerServiceSMOImpl.queryProductGroupOrderReturns(productGroupOrderReturnDto);
            refrashShopName(productGroupOrderReturnDtos);

        } else {
            productGroupOrderReturnDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, productGroupOrderReturnDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);
    }

    /**
     * 刷入店铺名称
     *
     * @param productGroupOrderReturnDtos
     */
    private void refrashShopName(List<ProductGroupOrderReturnDto> productGroupOrderReturnDtos) {
        if (ListUtil.isNull(productGroupOrderReturnDtos)) {
            return;
        }
        List<String> shopIds = new ArrayList<>();
        for (ProductGroupOrderReturnDto productGroupOrderReturnDto : productGroupOrderReturnDtos) {
            shopIds.add(productGroupOrderReturnDto.getShopId());
        }

        ShopDto shopDto = new ShopDto();
        shopDto.setShopIds(shopIds.toArray(new String[shopIds.size()]));
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);
        for (ProductGroupOrderReturnDto productGroupOrderReturnDto : productGroupOrderReturnDtos) {
            for (ShopDto tShopDto : shopDtos) {
                if (productGroupOrderReturnDto.getShopId().equals(tShopDto.getShopId())) {
                    productGroupOrderReturnDto.setShopName(tShopDto.getShopName());
                }
            }
        }
    }
}
